package com.jason.kotlin.gank.ui.adapter

import android.graphics.Bitmap
import android.view.ViewGroup
import android.widget.ImageView
import androidx.collection.ArrayMap
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.db.GankDaoManager
import com.jason.kotlin.gank.model.GankDataBean
import com.jason.library.common.util.ScreenUtils
import com.jason.library.widget.snackbar.MySnackbar
import com.ldoublem.thumbUplib.ThumbUpView
import com.ldoublem.thumbUplib.ThumbUpView.OnThumbUp

class GankGirlAdapter :
    BaseQuickAdapter<GankDataBean, BaseViewHolder>(R.layout.item_girl_staggered), LoadMoreModule {

    private val picSizeEntityArrayMap = ArrayMap<String, PicSizeEntity>()

    override fun convert(holder: BaseViewHolder, item: GankDataBean) {

        item.run {
            holder.setText(R.id.tvShowTime, desc)

            val screenWidth = ScreenUtils.getScreenWidth(context)

            val picSizeEntity: PicSizeEntity? = picSizeEntityArrayMap[url]
            if (picSizeEntity != null) {
                val width: Int = picSizeEntity.width
                val height: Int = picSizeEntity.height
                //计算高宽比
                val finalHeight: Int = screenWidth / 2 * height / width
                val layoutParams: ViewGroup.LayoutParams = holder.itemView.layoutParams
                layoutParams.height = finalHeight
            }

            val options = RequestOptions()
            options.fitCenter()
            options.placeholder(R.drawable.pic_gray_bg)
            options.diskCacheStrategy(DiskCacheStrategy.ALL)

            val image = holder.getView<ImageView>(R.id.image)

            Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .thumbnail(0.2f)
                .listener(object : RequestListener<Bitmap> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any,
                        target: Target<Bitmap>,
                        isFirstResource: Boolean
                    ): Boolean {
                        return false
                    }

                    override fun onResourceReady(
                        resource: Bitmap,
                        model: Any,
                        target: Target<Bitmap>,
                        dataSource: DataSource,
                        isFirstResource: Boolean
                    ): Boolean {
                        val picSizeEntity: PicSizeEntity? = picSizeEntityArrayMap.get(url)
                        if (picSizeEntity == null) {
                            val width = resource.width
                            val height = resource.height
                            //计算高宽比
                            val finalHeight: Int = screenWidth / 2 * height / width
                            val layoutParams: ViewGroup.LayoutParams =
                                holder.itemView.layoutParams
                            layoutParams.height = finalHeight
                            picSizeEntityArrayMap[url] = PicSizeEntity(width, height)
                        }
                        return false
                    }
                })
                .into(image)

            //查询是否存在收藏
            val isCollect: Boolean =
                GankDaoManager.getCollectDao().queryOneCollectByID(item.id)
            val btnCollect2 = holder.getView<ThumbUpView>(R.id.btn_collect2)
            if (isCollect) {
                btnCollect2.setLike()
            } else {
                btnCollect2.setUnlike()
            }

            btnCollect2.setOnThumbUp(OnThumbUp { like ->
                if (like) {
                    val insertResult: Boolean =
                        GankDaoManager.getCollectDao().insertOneCollect(item)
                    if (insertResult) {
                        MySnackbar.makeSnackBarBlack(btnCollect2, "收藏成功")
                    } else {
                        btnCollect2.setUnlike()
                        MySnackbar.makeSnackBarRed(btnCollect2, "收藏失败")
                    }
                } else {
                    val deleteResult: Boolean =
                        GankDaoManager.getCollectDao().deleteOneCollect(id)
                    if (deleteResult) {
                        MySnackbar.makeSnackBarBlack(btnCollect2, "取消收藏成功")
                    } else {
                        btnCollect2.setLike()
                        MySnackbar.makeSnackBarRed(btnCollect2, "取消收藏失败")
                    }
                }
            })
        }

    }


    data class PicSizeEntity(var width:Int, var height:Int)
}